package com.zds.support.util

import android.content.Context
import android.net.ConnectivityManager
import android.net.NetworkInfo

object NetUtils {
    @SuppressWarnings
    fun isWifiConnected(context: Context): Boolean {
        val cm = context.getSystemService(Context.CONNECTIVITY_SERVICE)
        if (cm != null && cm is ConnectivityManager) {


            /*  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                  val activeNetwork = cm.activeNetwork
                  if (activeNetwork != null) {
                      val networkCapabilities = cm.getNetworkCapabilities(activeNetwork)
                      return  networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED);
                  }
              } else {

              }*/
            val activeNetworkInfo = cm.activeNetworkInfo
            if (activeNetworkInfo != null) {
                return activeNetworkInfo.type == ConnectivityManager.TYPE_WIFI
            }

        }
        return false
    }

    // 判断网络是否可用
    @JvmStatic
    fun isNetConnected(): Boolean {
        return isNetConnected(AppUtils.appContext)
    }

    // 判断网络是否可用
    @JvmStatic
    fun isNetConnected(context: Context): Boolean {
        try {
            val result: Boolean
            val cm = context
                    .getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
            val netinfo = cm.activeNetworkInfo
            if (netinfo != null && netinfo.isConnected) {
                result = true
            } else {
                result = false
            }
            return result
        } catch (e: Exception) {

        }

        return true
    }

    @SuppressWarnings
    private fun getActiveNetworkInfo(context: Context): NetworkInfo? {
        val cm = context.getSystemService(Context.CONNECTIVITY_SERVICE)
        if (cm is ConnectivityManager) {
            return cm.activeNetworkInfo
        }
        return null
    }

    @JvmStatic
    fun getNetWorkTypeForVideoFreeze(context: Context): String {
        val info = getActiveNetworkInfo(context)
        var netType: String = ""
        if (info != null && info.isAvailable) {
            val type = info.type
            if (type == ConnectivityManager.TYPE_WIFI) {
                return "WIFI"
            } else if (type == ConnectivityManager.TYPE_MOBILE) {
                return "流量" + info.subtypeName
            }
        }
        return netType
    }
}